# 发送多种类型的邮件
import logging
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

import config


def send_email(title, content, retry=0):
    logging.info("title: {}, content: {}".format(title, content))
    msg_from = config.email_from  # 发送方邮箱
    passwd = config.email_passwd  # 就是上面的授权码


    # 设置邮件内容
    # MIMEMultipart类可以放任何内容
    msg = MIMEMultipart()
    # conntent = "这个是字符串"
    # 把内容加进去
    msg.attach(MIMEText(content, 'plain', 'utf-8'))

    # 设置邮件主题
    msg['Subject'] = "%s账号出现问题 这个是紧急邮件! 紧急状态!!!" % title

    # 发送方信息
    msg['From'] = msg_from

    # 开始发送

    # 通过SSL方式发送，服务器地址和端口
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    # 登录邮箱
    s.login(msg_from, passwd)
    # 开始发送
    try:
        s.sendmail(msg_from, config.to_email, msg.as_string())
        logging.info("{} 发送成功".format(title))
        return True
    except:
        if retry < 3:
            retry += 1
            logging.info("重试{}次".format(retry))
            send_email(title, content, retry)
        else:
            logging.info("{} 发送失败".format(title))
            return False


if __name__ == '__main__':
    send_email(123456, "\n".join(["余额:206454.72","净值:130047.92","已用预付款:19328.75","可用预付款:110719,17", "预付款比例:672.82"]))